
#include <document.h>
#include <stdio.h>
#include <glib/gstdio.h>
#include "utils.h"
#include "uimanager.h"
#include "document_manager.h"
#include "document_header.h"
#include "doc_panel_base.h"




static glong string_get_length (const char* self);
struct _DocumentPrivate {
	GtkScrolledWindow* scrolled_wnd;
	char* file_path;
	DocumentDocumentState doc_state;
	gboolean doc_panel_mutex;
};

#define DOCUMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_DOCUMENT, DocumentPrivate))
enum  {
	DOCUMENT_DUMMY_PROPERTY,
	DOCUMENT_STATE
};
GtkSourceLanguageManager* document_source_lang_manager = NULL;
static void document_auto_set_language (Document* self);
static gboolean document_real_on_focus_in_event (Document* self, GtkSourceView* source_view, const GdkEventFocus* event);
static gboolean document_real_on_show_doc_panel (Document* self);
static gboolean document_real_on_hide_doc_panel (Document* self);
static void _document_on_modified_changed_gtk_text_buffer_modified_changed (GtkSourceBuffer* _sender, gpointer self);
static gboolean _document_on_show_doc_panel_doc_panel_base_show_panel (GoToLinePanel* _sender, gpointer self);
static gboolean _document_on_hide_doc_panel_doc_panel_base_close_panel (GoToLinePanel* _sender, gpointer self);
static void _document_search_sensitive_update_gtk_text_buffer_changed (GtkSourceBuffer* _sender, gpointer self);
static gboolean _document_on_focus_in_event_gtk_widget_focus_in_event (GtkSourceView* _sender, const GdkEventFocus* event, gpointer self);
static GObject * document_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static gpointer document_parent_class = NULL;
static void document_finalize (GObject* obj);
static int _vala_strcmp0 (const char * str1, const char * str2);



static glong string_get_length (const char* self) {
	g_return_val_if_fail (self != NULL, 0L);
	return g_utf8_strlen (self, -1);
}



GType document_document_state_get_type (void) {
	static GType document_document_state_type_id = 0;
	if (G_UNLIKELY (document_document_state_type_id == 0)) {
		static const GEnumValue values[] = {{DOCUMENT_DOCUMENT_STATE_NO_MODIFIED, "DOCUMENT_DOCUMENT_STATE_NO_MODIFIED", "no-modified"}, {DOCUMENT_DOCUMENT_STATE_MODIFIED, "DOCUMENT_DOCUMENT_STATE_MODIFIED", "modified"}, {DOCUMENT_DOCUMENT_STATE_READ_ONLY, "DOCUMENT_DOCUMENT_STATE_READ_ONLY", "read-only"}, {0, NULL, NULL}};
		document_document_state_type_id = g_enum_register_static ("DocumentDocumentState", values);
	}
	return document_document_state_type_id;
}


/*Ustawia source_lang na podstawie mime type pliku file_path*/
static void document_auto_set_language (Document* self) {
	GError * inner_error;
	g_return_if_fail (self != NULL);
	inner_error = NULL;
	{
		char* doc_mime_type;
		GtkSourceLanguage* lang;
		const char** lang_ids;
		doc_mime_type = utils_get_mime_type (self->priv->file_path, &inner_error);
		if (inner_error != NULL) {
			goto __catch1_g_error;
			goto __finally1;
		}
		lang = NULL;
		lang_ids = (const char**) gtk_source_language_manager_get_language_ids (document_source_lang_manager);
		if (lang_ids != NULL) {
			{
				gint i;
				i = 0;
				for (; (*(lang_ids + i)) != NULL; (i = i + 1)) {
					const char* _tmp0;
					char* lang_id;
					GtkSourceLanguage* _tmp2;
					GtkSourceLanguage* _tmp1;
					const char** list;
					_tmp0 = NULL;
					lang_id = (_tmp0 = (char*) (*(lang_ids + i)), (_tmp0 == NULL) ? NULL : g_strdup (_tmp0));
					_tmp2 = NULL;
					_tmp1 = NULL;
					lang = (_tmp2 = (_tmp1 = gtk_source_language_manager_get_language (document_source_lang_manager, lang_id), (_tmp1 == NULL) ? NULL : g_object_ref (_tmp1)), (lang == NULL) ? NULL : (lang = (g_object_unref (lang), NULL)), _tmp2);
					list = gtk_source_language_get_mime_types (lang);
					if (list != NULL) {
						{
							gint j;
							j = 0;
							for (; (*(list + j)) != NULL; (j = j + 1)) {
								if (_vala_strcmp0 (doc_mime_type, *(list + j)) == 0) {
									GtkSourceLanguage* _tmp4;
									GtkSourceLanguage* _tmp3;
									_tmp4 = NULL;
									_tmp3 = NULL;
									self->source_lang = (_tmp4 = (_tmp3 = gtk_source_language_manager_get_language (document_source_lang_manager, lang_id), (_tmp3 == NULL) ? NULL : g_object_ref (_tmp3)), (self->source_lang == NULL) ? NULL : (self->source_lang = (g_object_unref (self->source_lang), NULL)), _tmp4);
									gtk_source_buffer_set_language (self->source_buff, self->source_lang);
									lang_id = (g_free (lang_id), NULL);
									doc_mime_type = (g_free (doc_mime_type), NULL);
									(lang == NULL) ? NULL : (lang = (g_object_unref (lang), NULL));
									return;
								}
							}
						}
					}
					lang_id = (g_free (lang_id), NULL);
				}
			}
		}
		doc_mime_type = (g_free (doc_mime_type), NULL);
		(lang == NULL) ? NULL : (lang = (g_object_unref (lang), NULL));
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError * er;
		er = inner_error;
		inner_error = NULL;
		{
			fprintf (stdout, "%s", er->message);
			(er == NULL) ? NULL : (er = (g_error_free (er), NULL));
		}
	}
	__finally1:
	if (inner_error != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, inner_error->message);
		g_clear_error (&inner_error);
		return;
	}
}


void document_set_contents_from_file (Document* self, const char* file_path) {
	GError * inner_error;
	char* _tmp1;
	const char* _tmp0;
	char* content;
	gint length;
	g_return_if_fail (self != NULL);
	g_return_if_fail (file_path != NULL);
	inner_error = NULL;
	_tmp1 = NULL;
	_tmp0 = NULL;
	self->priv->file_path = (_tmp1 = (_tmp0 = file_path, (_tmp0 == NULL) ? NULL : g_strdup (_tmp0)), self->priv->file_path = (g_free (self->priv->file_path), NULL), _tmp1);
	content = NULL;
	length = 0;
	{
		char* _tmp4;
		gboolean _tmp3;
		char* _tmp2;
		gboolean _tmp5;
		_tmp4 = NULL;
		_tmp2 = NULL;
		_tmp5 = (_tmp3 = g_file_get_contents (self->priv->file_path, &_tmp2, &length, &inner_error), content = (_tmp4 = _tmp2, content = (g_free (content), NULL), _tmp4), _tmp3);
		if (inner_error != NULL) {
			if (inner_error->domain == G_FILE_ERROR) {
				goto __catch2_g_file_error;
			}
			goto __finally2;
		}
		if (_tmp5) {
			char* _tmp6;
			char* _tmp7;
			gtk_text_buffer_set_text ((GtkTextBuffer*) self->source_buff, content, length);
			document_auto_set_language (self);
			gtk_text_buffer_set_modified ((GtkTextBuffer*) self->source_buff, FALSE);
			document_search_sensitive_update (self);
			_tmp6 = g_filename_to_uri (self->priv->file_path, NULL, &inner_error);
			if (inner_error != NULL) {
				if (inner_error->domain == G_FILE_ERROR) {
					goto __catch2_g_file_error;
				}
				goto __finally2;
			}
			_tmp7 = NULL;
			gtk_recent_manager_add_item (gtk_recent_manager_get_default (), _tmp7 = _tmp6);
			_tmp7 = (g_free (_tmp7), NULL);
		}
	}
	goto __finally2;
	__catch2_g_file_error:
	{
		GError * er;
		er = inner_error;
		inner_error = NULL;
		{
			fprintf (stdout, "Error %s\n", er->message);
			(er == NULL) ? NULL : (er = (g_error_free (er), NULL));
		}
	}
	__finally2:
	if (inner_error != NULL) {
		content = (g_free (content), NULL);
		g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, inner_error->message);
		g_clear_error (&inner_error);
		return;
	}
	content = (g_free (content), NULL);
}


char* document_get_content (Document* self) {
	GtkTextIter start = {0};
	GtkTextIter end = {0};
	const char* _tmp0;
	g_return_val_if_fail (self != NULL, NULL);
	gtk_text_buffer_get_start_iter ((GtkTextBuffer*) self->source_buff, &start);
	gtk_text_buffer_get_end_iter ((GtkTextBuffer*) self->source_buff, &end);
	_tmp0 = NULL;
	return (_tmp0 = gtk_text_buffer_get_text ((GtkTextBuffer*) self->source_buff, &start, &end, TRUE), (_tmp0 == NULL) ? NULL : g_strdup (_tmp0));
}


void document_on_modified_changed (Document* self) {
	DocumentDocumentState _tmp0;
	g_return_if_fail (self != NULL);
	_tmp0 = 0;
	if ((gtk_text_buffer_get_modified ((GtkTextBuffer*) self->source_buff))) {
		_tmp0 = DOCUMENT_DOCUMENT_STATE_MODIFIED;
	} else {
		_tmp0 = DOCUMENT_DOCUMENT_STATE_NO_MODIFIED;
	}
	self->priv->doc_state = _tmp0;
	document_header_set_state ((DOCUMENT_HEADER (gtk_notebook_get_tab_label ((GtkNotebook*) document_manager_get_instance (), (GtkWidget*) self))), document_get_state (self));
}


void document_search_sensitive_update (Document* self) {
	g_return_if_fail (self != NULL);
	if (gtk_text_buffer_get_char_count ((GtkTextBuffer*) self->source_buff) == 0) {
		UIManager* _tmp0;
		UIManager* _tmp1;
		UIManager* _tmp2;
		_tmp0 = NULL;
		gtk_action_set_sensitive (ui_manager_get_action (_tmp0 = ui_manager_get_instance (), "search_find"), FALSE);
		(_tmp0 == NULL) ? NULL : (_tmp0 = (g_object_unref (_tmp0), NULL));
		_tmp1 = NULL;
		gtk_action_set_sensitive (ui_manager_get_action (_tmp1 = ui_manager_get_instance (), "search_replace"), FALSE);
		(_tmp1 == NULL) ? NULL : (_tmp1 = (g_object_unref (_tmp1), NULL));
		_tmp2 = NULL;
		gtk_action_set_sensitive (ui_manager_get_action (_tmp2 = ui_manager_get_instance (), "search_goto_line"), FALSE);
		(_tmp2 == NULL) ? NULL : (_tmp2 = (g_object_unref (_tmp2), NULL));
	} else {
		if (gtk_text_buffer_get_char_count ((GtkTextBuffer*) self->source_buff) >= 1) {
			UIManager* _tmp3;
			UIManager* _tmp4;
			UIManager* _tmp5;
			_tmp3 = NULL;
			gtk_action_set_sensitive (ui_manager_get_action (_tmp3 = ui_manager_get_instance (), "search_find"), TRUE);
			(_tmp3 == NULL) ? NULL : (_tmp3 = (g_object_unref (_tmp3), NULL));
			_tmp4 = NULL;
			gtk_action_set_sensitive (ui_manager_get_action (_tmp4 = ui_manager_get_instance (), "search_replace"), TRUE);
			(_tmp4 == NULL) ? NULL : (_tmp4 = (g_object_unref (_tmp4), NULL));
			_tmp5 = NULL;
			gtk_action_set_sensitive (ui_manager_get_action (_tmp5 = ui_manager_get_instance (), "search_goto_line"), TRUE);
			(_tmp5 == NULL) ? NULL : (_tmp5 = (g_object_unref (_tmp5), NULL));
		}
	}
}


void document_insert_text_at_cursor (Document* self, const char* text) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (text != NULL);
	gtk_text_buffer_insert_at_cursor ((GtkTextBuffer*) self->source_buff, text, (gint) string_get_length (text));
}


Document* document_construct (GType object_type, const char* file_path) {
	Document * self;
	char* _tmp1;
	const char* _tmp0;
	self = g_object_newv (object_type, 0, NULL);
	_tmp1 = NULL;
	_tmp0 = NULL;
	self->priv->file_path = (_tmp1 = (_tmp0 = file_path, (_tmp0 == NULL) ? NULL : g_strdup (_tmp0)), self->priv->file_path = (g_free (self->priv->file_path), NULL), _tmp1);
	if (self->priv->file_path != NULL) {
		document_set_contents_from_file (self, self->priv->file_path);
	}
	gtk_widget_show_all ((GtkWidget*) self);
	return self;
}


Document* document_new (const char* file_path) {
	return document_construct (TYPE_DOCUMENT, file_path);
}


gboolean document_save (Document* self) {
	GError * inner_error;
	g_return_val_if_fail (self != NULL, FALSE);
	inner_error = NULL;
	if (self->priv->file_path != NULL) {
		{
			GtkTextIter start = {0};
			GtkTextIter end = {0};
			gtk_text_buffer_get_start_iter ((GtkTextBuffer*) self->source_buff, &start);
			gtk_text_buffer_get_end_iter ((GtkTextBuffer*) self->source_buff, &end);
			g_file_set_contents (self->priv->file_path, gtk_text_buffer_get_text ((GtkTextBuffer*) self->source_buff, &start, &end, TRUE), -1, &inner_error);
			if (inner_error != NULL) {
				if (inner_error->domain == G_FILE_ERROR) {
					goto __catch3_g_file_error;
				}
				goto __finally3;
			}
			if (self->source_lang == NULL) {
				document_auto_set_language (self);
			}
			gtk_text_buffer_set_modified ((GtkTextBuffer*) self->source_buff, FALSE);
		}
		goto __finally3;
		__catch3_g_file_error:
		{
			GError * er;
			er = inner_error;
			inner_error = NULL;
			{
				gboolean _tmp0;
				return (_tmp0 = FALSE, (er == NULL) ? NULL : (er = (g_error_free (er), NULL)), _tmp0);
			}
		}
		__finally3:
		if (inner_error != NULL) {
			g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, inner_error->message);
			g_clear_error (&inner_error);
			return FALSE;
		}
	} else {
		document_save_as (self);
	}
	return TRUE;
}


gboolean document_save_as (Document* self) {
	GtkFileChooserDialog* dlg;
	gboolean _tmp5;
	g_return_val_if_fail (self != NULL, FALSE);
	dlg = g_object_ref_sink ((GtkFileChooserDialog*) gtk_file_chooser_dialog_new ("Zapisz jako", NULL, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL));
	if (gtk_dialog_run ((GtkDialog*) dlg) == GTK_RESPONSE_ACCEPT) {
		char* _tmp1;
		const char* _tmp0;
		_tmp1 = NULL;
		_tmp0 = NULL;
		self->priv->file_path = (_tmp1 = (_tmp0 = gtk_file_chooser_get_filename ((GtkFileChooser*) dlg), (_tmp0 == NULL) ? NULL : g_strdup (_tmp0)), self->priv->file_path = (g_free (self->priv->file_path), NULL), _tmp1);
		if (document_save (self)) {
			char* _tmp2;
			_tmp2 = NULL;
			document_header_set_text ((DOCUMENT_HEADER (gtk_notebook_get_tab_label ((GtkNotebook*) document_manager_get_instance (), (GtkWidget*) self))), _tmp2 = g_path_get_basename (self->priv->file_path));
			_tmp2 = (g_free (_tmp2), NULL);
			gtk_recent_manager_add_item (gtk_recent_manager_get_default (), gtk_file_chooser_get_uri ((GtkFileChooser*) dlg));
		} else {
			char* _tmp3;
			_tmp3 = NULL;
			self->priv->file_path = (_tmp3 = NULL, self->priv->file_path = (g_free (self->priv->file_path), NULL), _tmp3);
		}
	} else {
		gboolean _tmp4;
		gtk_object_destroy ((GtkObject*) dlg);
		return (_tmp4 = FALSE, (dlg == NULL) ? NULL : (dlg = (g_object_unref (dlg), NULL)), _tmp4);
	}
	gtk_object_destroy ((GtkObject*) dlg);
	return (_tmp5 = TRUE, (dlg == NULL) ? NULL : (dlg = (g_object_unref (dlg), NULL)), _tmp5);
}


gboolean document_close (Document* self) {
	g_return_val_if_fail (self != NULL, FALSE);
	if (gtk_text_buffer_get_modified ((GtkTextBuffer*) self->source_buff)) {
		const char* _tmp0;
		char* _tmp2;
		char* _tmp1;
		GtkMessageDialog* _tmp3;
		GtkMessageDialog* msg;
		gint response;
		_tmp0 = NULL;
		if ((self->priv->file_path != NULL)) {
			_tmp0 = self->priv->file_path;
		} else {
			_tmp0 = document_header_get_text ((DOCUMENT_HEADER (gtk_notebook_get_tab_label ((GtkNotebook*) document_manager_get_instance (), (GtkWidget*) self))));
		}
		_tmp2 = NULL;
		_tmp1 = NULL;
		_tmp3 = NULL;
		msg = (_tmp3 = g_object_ref_sink ((GtkMessageDialog*) gtk_message_dialog_new_with_markup (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _tmp2 = g_strconcat (_tmp1 = g_strconcat ("Plik \"<b>", (_tmp0), NULL), "\"</b> został zmodyfikowany !", NULL), NULL)), _tmp2 = (g_free (_tmp2), NULL), _tmp1 = (g_free (_tmp1), NULL), _tmp3);
		gtk_dialog_add_button ((GtkDialog*) msg, GTK_STOCK_YES, (gint) GTK_RESPONSE_YES);
		gtk_dialog_add_button ((GtkDialog*) msg, GTK_STOCK_NO, (gint) GTK_RESPONSE_NO);
		gtk_dialog_add_button ((GtkDialog*) msg, GTK_STOCK_CANCEL, (gint) GTK_RESPONSE_CANCEL);
		gtk_message_dialog_format_secondary_text (msg, "Czy chcesz zapisać zmiany ?", NULL);
		response = gtk_dialog_run ((GtkDialog*) msg);
		gtk_object_destroy ((GtkObject*) msg);
		if (response == GTK_RESPONSE_YES) {
			document_save (self);
		} else {
			if (response == GTK_RESPONSE_CANCEL) {
				gboolean _tmp4;
				return (_tmp4 = FALSE, (msg == NULL) ? NULL : (msg = (g_object_unref (msg), NULL)), _tmp4);
			}
		}
		(msg == NULL) ? NULL : (msg = (g_object_unref (msg), NULL));
	}
	return TRUE;
}


inline void document_undo (Document* self) {
	gboolean _tmp0;
	g_return_if_fail (self != NULL);
	if ((g_object_get (self->source_buff, "can-undo", &_tmp0, NULL), _tmp0)) {
		gtk_source_buffer_undo (self->source_buff);
	}
}


inline void document_redo (Document* self) {
	gboolean _tmp0;
	g_return_if_fail (self != NULL);
	if ((g_object_get (self->source_buff, "can-redo", &_tmp0, NULL), _tmp0)) {
		gtk_source_buffer_redo (self->source_buff);
	}
}


inline void document_cut (Document* self) {
	GtkClipboard* clipboard;
	GtkClipboard* _tmp1;
	GtkClipboard* _tmp0;
	g_return_if_fail (self != NULL);
	clipboard = NULL;
	_tmp1 = NULL;
	_tmp0 = NULL;
	clipboard = (_tmp1 = (_tmp0 = gtk_widget_get_clipboard ((GtkWidget*) self->source_view, GDK_SELECTION_CLIPBOARD), (_tmp0 == NULL) ? NULL : g_object_ref (_tmp0)), (clipboard == NULL) ? NULL : (clipboard = (g_object_unref (clipboard), NULL)), _tmp1);
	gtk_text_buffer_cut_clipboard ((GtkTextBuffer*) self->source_buff, clipboard, TRUE);
	(clipboard == NULL) ? NULL : (clipboard = (g_object_unref (clipboard), NULL));
}


inline void document_copy (Document* self) {
	GtkClipboard* clipboard;
	GtkClipboard* _tmp1;
	GtkClipboard* _tmp0;
	g_return_if_fail (self != NULL);
	clipboard = NULL;
	_tmp1 = NULL;
	_tmp0 = NULL;
	clipboard = (_tmp1 = (_tmp0 = gtk_widget_get_clipboard ((GtkWidget*) self->source_view, GDK_SELECTION_CLIPBOARD), (_tmp0 == NULL) ? NULL : g_object_ref (_tmp0)), (clipboard == NULL) ? NULL : (clipboard = (g_object_unref (clipboard), NULL)), _tmp1);
	gtk_text_buffer_copy_clipboard ((GtkTextBuffer*) self->source_buff, clipboard);
	(clipboard == NULL) ? NULL : (clipboard = (g_object_unref (clipboard), NULL));
}


inline void document_paste (Document* self) {
	GtkClipboard* clipboard;
	GtkClipboard* _tmp1;
	GtkClipboard* _tmp0;
	g_return_if_fail (self != NULL);
	clipboard = NULL;
	_tmp1 = NULL;
	_tmp0 = NULL;
	clipboard = (_tmp1 = (_tmp0 = gtk_widget_get_clipboard ((GtkWidget*) self->source_view, GDK_SELECTION_CLIPBOARD), (_tmp0 == NULL) ? NULL : g_object_ref (_tmp0)), (clipboard == NULL) ? NULL : (clipboard = (g_object_unref (clipboard), NULL)), _tmp1);
	gtk_text_buffer_paste_clipboard ((GtkTextBuffer*) self->source_buff, clipboard, NULL, TRUE);
	(clipboard == NULL) ? NULL : (clipboard = (g_object_unref (clipboard), NULL));
}


inline void document_delete_selection (Document* self) {
	g_return_if_fail (self != NULL);
	gtk_text_buffer_delete_selection ((GtkTextBuffer*) self->source_buff, FALSE, FALSE);
}


inline void document_select_all (Document* self) {
	GtkTextIter start = {0};
	GtkTextIter end = {0};
	g_return_if_fail (self != NULL);
	gtk_text_buffer_get_iter_at_offset ((GtkTextBuffer*) self->source_buff, &start, 0);
	gtk_text_buffer_get_end_iter ((GtkTextBuffer*) self->source_buff, &end);
	gtk_text_buffer_select_range ((GtkTextBuffer*) self->source_buff, &start, &end);
}


inline void document_grab_focus (Document* self) {
	g_return_if_fail (self != NULL);
	gtk_widget_grab_focus ((GtkWidget*) self->source_view);
}


void document_show_search_panel (Document* self) {
	gboolean _tmp0;
	g_return_if_fail (self != NULL);
	if (!(g_object_get ((GtkWidget*) self->search_panel, "visible", &_tmp0, NULL), _tmp0)) {
		gboolean _tmp1;
		if ((g_object_get ((GtkWidget*) self->replace_panel, "visible", &_tmp1, NULL), _tmp1)) {
			search_panel_set_match_case (self->search_panel, search_panel_get_match_case ((SearchPanel*) self->replace_panel));
			search_panel_set_match_words (self->search_panel, search_panel_get_match_words ((SearchPanel*) self->replace_panel));
			search_panel_set_match_regex (self->search_panel, search_panel_get_match_regex ((SearchPanel*) self->replace_panel));
			search_panel_set_search_pharse (self->search_panel, search_panel_get_search_pharse ((SearchPanel*) self->replace_panel));
			doc_panel_base_close ((DocPanelBase*) self->replace_panel);
		}
		doc_panel_base_show ((DocPanelBase*) self->search_panel);
	} else {
		doc_panel_base_close ((DocPanelBase*) self->search_panel);
	}
}


void document_show_replace_panel (Document* self) {
	gboolean _tmp0;
	g_return_if_fail (self != NULL);
	if (!(g_object_get ((GtkWidget*) self->replace_panel, "visible", &_tmp0, NULL), _tmp0)) {
		gboolean _tmp1;
		if ((g_object_get ((GtkWidget*) self->search_panel, "visible", &_tmp1, NULL), _tmp1)) {
			search_panel_set_match_case ((SearchPanel*) self->replace_panel, search_panel_get_match_case (self->search_panel));
			search_panel_set_match_words ((SearchPanel*) self->replace_panel, search_panel_get_match_words (self->search_panel));
			search_panel_set_match_regex ((SearchPanel*) self->replace_panel, search_panel_get_match_regex (self->search_panel));
			search_panel_set_search_pharse ((SearchPanel*) self->replace_panel, search_panel_get_search_pharse (self->search_panel));
			doc_panel_base_close ((DocPanelBase*) self->search_panel);
		}
		doc_panel_base_show ((DocPanelBase*) self->replace_panel);
	} else {
		doc_panel_base_close ((DocPanelBase*) self->replace_panel);
	}
}


void document_goto_next_found_match (Document* self) {
	g_return_if_fail (self != NULL);
	search_panel_goto_next_match (self->search_panel);
}


void document_goto_prev_found_match (Document* self) {
	g_return_if_fail (self != NULL);
	search_panel_goto_prev_match (self->search_panel);
}


void document_show_goto_line_panel (Document* self) {
	gboolean _tmp0;
	g_return_if_fail (self != NULL);
	if (!(g_object_get ((GtkWidget*) self->goto_line_panel, "visible", &_tmp0, NULL), _tmp0)) {
		doc_panel_base_show ((DocPanelBase*) self->goto_line_panel);
	} else {
		doc_panel_base_close ((DocPanelBase*) self->goto_line_panel);
	}
}


static gboolean document_real_on_focus_in_event (Document* self, GtkSourceView* source_view, const GdkEventFocus* event) {
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (source_view != NULL, FALSE);
	document_search_sensitive_update (self);
	return FALSE;
}


gboolean document_on_focus_in_event (Document* self, GtkSourceView* source_view, const GdkEventFocus* event) {
	return DOCUMENT_GET_CLASS (self)->on_focus_in_event (self, source_view, event);
}


static gboolean document_real_on_show_doc_panel (Document* self) {
	gboolean _tmp0;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0 = FALSE;
	if (!self->priv->doc_panel_mutex) {
		_tmp0 = gtk_text_buffer_get_char_count ((GtkTextBuffer*) self->source_buff) != 0;
	} else {
		_tmp0 = FALSE;
	}
	if (_tmp0) {
		self->priv->doc_panel_mutex = TRUE;
		return FALSE;
	}
	return TRUE;
}


gboolean document_on_show_doc_panel (Document* self) {
	return DOCUMENT_GET_CLASS (self)->on_show_doc_panel (self);
}


static gboolean document_real_on_hide_doc_panel (Document* self) {
	g_return_val_if_fail (self != NULL, FALSE);
	self->priv->doc_panel_mutex = FALSE;
	return FALSE;
}


gboolean document_on_hide_doc_panel (Document* self) {
	return DOCUMENT_GET_CLASS (self)->on_hide_doc_panel (self);
}


DocumentDocumentState document_get_state (Document* self) {
	g_return_val_if_fail (self != NULL, 0);
	return self->priv->doc_state;
}


static void _document_on_modified_changed_gtk_text_buffer_modified_changed (GtkSourceBuffer* _sender, gpointer self) {
	document_on_modified_changed (self);
}


static gboolean _document_on_show_doc_panel_doc_panel_base_show_panel (GoToLinePanel* _sender, gpointer self) {
	return document_on_show_doc_panel (self);
}


static gboolean _document_on_hide_doc_panel_doc_panel_base_close_panel (GoToLinePanel* _sender, gpointer self) {
	return document_on_hide_doc_panel (self);
}


static void _document_search_sensitive_update_gtk_text_buffer_changed (GtkSourceBuffer* _sender, gpointer self) {
	document_search_sensitive_update (self);
}


static gboolean _document_on_focus_in_event_gtk_widget_focus_in_event (GtkSourceView* _sender, const GdkEventFocus* event, gpointer self) {
	return document_on_focus_in_event (self, _sender, event);
}


static GObject * document_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	DocumentClass * klass;
	GObjectClass * parent_class;
	Document * self;
	klass = DOCUMENT_CLASS (g_type_class_peek (TYPE_DOCUMENT));
	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DOCUMENT (obj);
	{
		GtkSourceBuffer* _tmp1;
		GtkTextTagTable* _tmp0;
		GtkSourceView* _tmp2;
		GtkHSeparator* _tmp3;
		GoToLinePanel* _tmp4;
		SearchPanel* _tmp5;
		ReplacePanel* _tmp6;
		_tmp1 = NULL;
		_tmp0 = NULL;
		self->source_buff = (_tmp1 = gtk_source_buffer_new (_tmp0 = gtk_text_tag_table_new ()), (self->source_buff == NULL) ? NULL : (self->source_buff = (g_object_unref (self->source_buff), NULL)), _tmp1);
		(_tmp0 == NULL) ? NULL : (_tmp0 = (g_object_unref (_tmp0), NULL));
		g_signal_connect_object ((GtkTextBuffer*) self->source_buff, "modified-changed", (GCallback) _document_on_modified_changed_gtk_text_buffer_modified_changed, self, 0);
		_tmp2 = NULL;
		self->source_view = (_tmp2 = g_object_ref_sink ((GtkSourceView*) gtk_source_view_new_with_buffer (self->source_buff)), (self->source_view == NULL) ? NULL : (self->source_view = (g_object_unref (self->source_view), NULL)), _tmp2);
		gtk_scrolled_window_set_policy (self->priv->scrolled_wnd, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
		gtk_container_add ((GtkContainer*) self->priv->scrolled_wnd, (GtkWidget*) self->source_view);
		gtk_box_pack_start_defaults ((GtkBox*) self, (GtkWidget*) self->priv->scrolled_wnd);
		_tmp3 = NULL;
		gtk_box_pack_start ((GtkBox*) self, (GtkWidget*) (_tmp3 = g_object_ref_sink ((GtkHSeparator*) gtk_hseparator_new ())), FALSE, FALSE, (guint) 0);
		(_tmp3 == NULL) ? NULL : (_tmp3 = (g_object_unref (_tmp3), NULL));
		_tmp4 = NULL;
		self->goto_line_panel = (_tmp4 = g_object_ref_sink (go_to_line_panel_new (&self->source_buff, &self->source_view)), (self->goto_line_panel == NULL) ? NULL : (self->goto_line_panel = (g_object_unref (self->goto_line_panel), NULL)), _tmp4);
		g_signal_connect_object ((DocPanelBase*) self->goto_line_panel, "show-panel", (GCallback) _document_on_show_doc_panel_doc_panel_base_show_panel, self, 0);
		g_signal_connect_object ((DocPanelBase*) self->goto_line_panel, "close-panel", (GCallback) _document_on_hide_doc_panel_doc_panel_base_close_panel, self, 0);
		gtk_box_pack_start ((GtkBox*) self, (GtkWidget*) self->goto_line_panel, FALSE, FALSE, (guint) 0);
		_tmp5 = NULL;
		self->search_panel = (_tmp5 = g_object_ref_sink (search_panel_new (&self->source_buff, &self->source_view)), (self->search_panel == NULL) ? NULL : (self->search_panel = (g_object_unref (self->search_panel), NULL)), _tmp5);
		g_signal_connect_object ((DocPanelBase*) self->search_panel, "show-panel", (GCallback) _document_on_show_doc_panel_doc_panel_base_show_panel, self, 0);
		g_signal_connect_object ((DocPanelBase*) self->search_panel, "close-panel", (GCallback) _document_on_hide_doc_panel_doc_panel_base_close_panel, self, 0);
		gtk_box_pack_start ((GtkBox*) self, (GtkWidget*) self->search_panel, FALSE, FALSE, (guint) 0);
		_tmp6 = NULL;
		self->replace_panel = (_tmp6 = g_object_ref_sink (replace_panel_new (&self->source_buff, &self->source_view)), (self->replace_panel == NULL) ? NULL : (self->replace_panel = (g_object_unref (self->replace_panel), NULL)), _tmp6);
		g_signal_connect_object ((DocPanelBase*) self->replace_panel, "show-panel", (GCallback) _document_on_show_doc_panel_doc_panel_base_show_panel, self, 0);
		g_signal_connect_object ((DocPanelBase*) self->replace_panel, "close-panel", (GCallback) _document_on_hide_doc_panel_doc_panel_base_close_panel, self, 0);
		gtk_box_pack_start ((GtkBox*) self, (GtkWidget*) self->replace_panel, FALSE, FALSE, (guint) 0);
		g_signal_connect_object ((GtkTextBuffer*) self->source_buff, "changed", (GCallback) _document_search_sensitive_update_gtk_text_buffer_changed, self, 0);
		g_signal_connect_object ((GtkWidget*) self->source_view, "focus-in-event", (GCallback) _document_on_focus_in_event_gtk_widget_focus_in_event, self, 0);
	}
	return obj;
}


static void document_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	Document * self;
	gpointer boxed;
	self = DOCUMENT (object);
	switch (property_id) {
		case DOCUMENT_STATE:
		g_value_set_enum (value, document_get_state (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void document_class_init (DocumentClass * klass) {
	document_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (DocumentPrivate));
	G_OBJECT_CLASS (klass)->get_property = document_get_property;
	G_OBJECT_CLASS (klass)->constructor = document_constructor;
	G_OBJECT_CLASS (klass)->finalize = document_finalize;
	DOCUMENT_CLASS (klass)->on_focus_in_event = document_real_on_focus_in_event;
	DOCUMENT_CLASS (klass)->on_show_doc_panel = document_real_on_show_doc_panel;
	DOCUMENT_CLASS (klass)->on_hide_doc_panel = document_real_on_hide_doc_panel;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_STATE, g_param_spec_enum ("state", "state", "state", DOCUMENT_TYPE_DOCUMENT_STATE, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
	document_source_lang_manager = gtk_source_language_manager_new ();
}


static void document_instance_init (Document * self) {
	self->priv = DOCUMENT_GET_PRIVATE (self);
	self->source_lang = NULL;
	self->priv->scrolled_wnd = g_object_ref_sink ((GtkScrolledWindow*) gtk_scrolled_window_new (NULL, NULL));
	self->priv->file_path = NULL;
	self->priv->doc_panel_mutex = FALSE;
}


static void document_finalize (GObject* obj) {
	Document * self;
	self = DOCUMENT (obj);
	(self->source_lang == NULL) ? NULL : (self->source_lang = (g_object_unref (self->source_lang), NULL));
	(self->source_buff == NULL) ? NULL : (self->source_buff = (g_object_unref (self->source_buff), NULL));
	(self->source_view == NULL) ? NULL : (self->source_view = (g_object_unref (self->source_view), NULL));
	(self->goto_line_panel == NULL) ? NULL : (self->goto_line_panel = (g_object_unref (self->goto_line_panel), NULL));
	(self->search_panel == NULL) ? NULL : (self->search_panel = (g_object_unref (self->search_panel), NULL));
	(self->replace_panel == NULL) ? NULL : (self->replace_panel = (g_object_unref (self->replace_panel), NULL));
	(self->priv->scrolled_wnd == NULL) ? NULL : (self->priv->scrolled_wnd = (g_object_unref (self->priv->scrolled_wnd), NULL));
	self->priv->file_path = (g_free (self->priv->file_path), NULL);
	G_OBJECT_CLASS (document_parent_class)->finalize (obj);
}


GType document_get_type (void) {
	static GType document_type_id = 0;
	if (document_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (DocumentClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) document_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Document), 0, (GInstanceInitFunc) document_instance_init, NULL };
		document_type_id = g_type_register_static (GTK_TYPE_VBOX, "Document", &g_define_type_info, 0);
	}
	return document_type_id;
}


static int _vala_strcmp0 (const char * str1, const char * str2) {
	if (str1 == NULL) {
		return -(str1 != str2);
	}
	if (str2 == NULL) {
		return str1 != str2;
	}
	return strcmp (str1, str2);
}




